package fanshe.load;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MyLoader extends ClassLoader {

	// 加载D盘下的类文件LoadedOne.class
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
        
        String filename = "d:/" + name + ".class";
        
        File file = new File(filename);
        System.out.println("执行类加载：\nfile.length=" + file.length() + ", lastModified=" + file.lastModified());
        byte[] fb = new byte[(int) file.length()];
        
        try (FileInputStream fis = new FileInputStream(file);) {
        	fis.read(fb);
        	System.out.println("fb:[0-9]");
        	System.out.printf("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", fb[0], fb[1], fb[2], fb[3], fb[4]);
        	System.out.printf("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", fb[5], fb[6], fb[7], fb[8], fb[9]);
        	
        	return defineClass(name, fb, 0, fb.length);
        } catch (IOException e) {
        	throw new ClassNotFoundException(name);
        }
    }
	
}
